<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Offhours extends MY_Controller {

	var $path;	
	var $days_arr;
	function __construct() {
		parent::__construct();
		$this->require_login();
		$this->load->model('skill_model', 'skill');
		$this->load->model('offhours_model', 'offhours');
		$this->path = '/recording/IVR/';
		
		$this->days_arr = array(
								'sat' => 'Saturday', 
								'sun' => 'Sunday', 
								'mon' => 'Monday', 
								'tue' => 'Tuesday',
								'wed' => 'Wednesday', 
								'thu' => 'Thursday', 
								'fri' => 'Friday'
								);
		
	}

	function index() 
	{
		check_permission("offhours", "view");
		$data = array();
		$data['page_heading'] = 'Off Hours Days';
		$GET = $this->input->get(NULL, true);
 		$this->config->set_item('enable_query_strings', TRUE);
		$config['base_url'] = base_url().$this->uri->segment(1).'/?';
		$config['per_page'] = '20';
 		$config['num_links'] = 5;	
		$start = $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$results = $this->offhours->getListingData($GET,$start);
		$data['start'] = $start;
		$data['results'] = $results['results'];
		$data['total_record'] = $results['total_record'];
		
		if(is_array(@$GET))	{
			unset($GET['per_page']);
 			$qs = http_build_query($GET); 
		} else {
			$qs = '';	
		}
		
		if(isset($qs) && !empty($qs)){
		   $this->pagination->suffix = "&".$qs;	
		}
		
		$config['total_rows'] = $data['total_record'];
		$config['full_tag_open'] = '<ul class="paging">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$perPage = $config['per_page'];
		$this->config->set_item('enable_query_strings', TRUE);
		$this->pagination->initialize($config);
 		
		$this->load->view($this->uri->segment(1).'/listing', $data);
	}
	
	function save($id = 0) 
	{
		$id = (int)$id;
		$POST = $this->input->post(NULL, TRUE);
		$data = array();
		$error_arr = array();
		$data['page_heading'] = ($id > 0) ? "Edit Off Hours Days" : "Add Off Hours Days";
		if( $id > 0 ) {
			check_permission("offhours", "edit");
		} else {
			check_permission("offhours", "add");
		}
		
		$this->form_validation->set_rules('skill_id', 'Skill', 'trim|required|callback_check_duplicate['.$id.']|xss_clean');
		$this->form_validation->set_rules('ext', 'Extension', 'trim|required|xss_clean');
		$this->form_validation->set_rules('off_start_time', 'OFF Start Time', 'trim|required|callback_checkValidTime|xss_clean');
		$this->form_validation->set_rules('off_end_time', 'OFF End Time', 'trim|required|callback_checkValidTime|xss_clean');
		$this->form_validation->set_rules('play_file', 'Play File', 'trim|required|callback_playback_validation|xss_clean');
		$this->form_validation->set_rules('timezone', 'Time Zone', 'trim|required|xss_clean');
		
		if( $this->input->post('sbmt') == 1 ) 
		{
			if( ! isset($POST['sun']) && ! isset($POST['mon']) && ! isset($POST['tue']) && ! isset($POST['wed']) && ! isset($POST['thu']) && ! isset($POST['fri']) )
			{	
				$error_arr[] = 'Please select at least one option';
			}
		}
		
		if ($this->form_validation->run() == FALSE || count($error_arr) > 0)
		{
			if ( $id > 0 )
			{
				$detail = $this->offhours->getDetailByID( $id );
				foreach($detail as $k=>$v):
				$data[$k] = $v;
				endforeach;
				$data['id'] = $id;
				$data['play_file'] = str_replace($this->path, '', $data['play_file']);
 			}// end if
			$data['error_arr'] = $error_arr;
 			$this->load->view($this->uri->segment(1).'/save', $data);
		} 
		else 
		{
				$record	= array( 
 							'skill_id' 			=> (int)$this->input->post('skill_id'),
							'ext' 				=> addslashes($this->input->post('ext')),
							'off_start_time'	=> addslashes($this->input->post('off_start_time')),
							'off_end_time'  	=> addslashes($this->input->post('off_end_time')),
							'play_file' 		=> addslashes($this->path.$this->input->post('play_file')),
							'sat'  				=> $this->input->post('sat') ? 'Y' : 'N',
							'sun'  				=> $this->input->post('sun') ? 'Y' : 'N',
							'mon'  				=> $this->input->post('mon') ? 'Y' : 'N',
							'tue'  				=> $this->input->post('tue') ? 'Y' : 'N',
							'wed'  				=> $this->input->post('wed') ? 'Y' : 'N',
							'thu'  				=> $this->input->post('thu') ? 'Y' : 'N',
							'fri'  				=> $this->input->post('fri') ? 'Y' : 'N',
							'timezone' 			=> addslashes($this->input->post('timezone'))
						  );	
						
			$this->offhours->save($record, $id);
						  
			if( $id > 0 ) 
			{
				$this->session->set_flashdata('userMesg', 'Record has been updated successfully.');
			} 
			else 
			{
				$this->session->set_flashdata('userMesg', 'Record has been added successfully.');
			}				
			
			redirect($this->uri->segment(1).'/');
		}
 
 	}// end save
  
 	function check_duplicate($skill_id, $id ="")
    {
		$POST = $this->input->post(NULL, TRUE);
		
		if( intval($POST['skill_id']) > 0 && $POST['off_start_time'] != '' && $POST['off_end_time'] != '' )
		{
			$where = " skill_id = ".intval($POST['skill_id'])." AND ";
			$where .= " off_start_time = ".$this->db->escape($POST['off_start_time'])." AND ";
			$where .= " off_end_time = ".$this->db->escape($POST['off_end_time'])." ";
			
			if( $id > 0 ) {
				$where .= " AND id <> ".$id;
			}
		
			if(! $this->common_model->record_exist("offhours_days", $where))
			{
				return true;	
			}
			else
			{
				$this->form_validation->set_message('check_duplicate', 'The Start and End Times must be unique.');
				return false;
			}
		}

    }// end check_duplicate
	
	function playback_validation($filename)
	{
		//return true;
		$path = $this->path;
		$filename = trim($filename);
		if( $filename != '') 
		{
			if( ! file_exists($path.$filename.".wav") ) 
			{
				$this->form_validation->set_message('playback_validation', 'The %s does not exist. Please place the file on above path');
				return false;
			}
			else
			{
				return true;	
			}
		}
	}

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */